package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.MemberMapper;
import com.atguigu.dao.OrderMapper;
import com.atguigu.service.ReportService;
import com.atguigu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * -- 今天新增会员数
     *  select count(*) from t_member where regTime = '2021-06-02'
     * -- 总会员数
     *  select count(*) from t_member
     * -- 本周新增会员数(>=本周的周一的日期)
     *  select count(*) from t_member where regTime between '2021-05-31' and '2021-06-06'
     * -- 本月新增会员数(>=本月的第一天的日期)
     *  select count(*) from t_member where regTime between '2021-06-01' and '2021-06-30'
     * -------------------------------------------------------------------------------
     * -- 今日预约数
     *  select count(*) from t_order where orderDate = '2021-06-02'
     * -- 今日已出游数
     *  select count(*) from t_order where orderDate = '2021-06-02' and orderStatus = '已出游'
     * -- 本周预约数(>=本周的周一的日期 <=本周的周日的日期)
     *  select count(*) from t_order where orderDate between '2021-05-31' and '2021-06-06'
     * -- 本周已出游数
     *  select count(*) from t_order where orderDate between '2021-05-31' and '2021-06-06' and orderStatus = '已出游'
     * -- 本月预约数(>=每月的第一天的日期 <=每月的最后一天的日期)
     *  select count(*) from t_order where orderDate between '2021-06-01' and '2021-06-30'
     * -- 本月已出游数
     *  select count(*) from t_order where orderDate between '2021-06-01' and '2021-06-30' and orderStatus = '已出游'
     * -- 热门套餐
     *
     */

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String,Object> map = null;
        try {
            //获取今天的日期
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获取本周一的日期
            String weekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本周日的日期
            String weekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
            //本月（1号）
            String monthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //本月（31号）
            String monthLast = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

            //将周一周日封装到map集合中
            Map<String,Object> paramWeek = new HashMap<String,Object>();
            paramWeek.put("weekMonday",weekMonday);
            paramWeek.put("weekSunday",weekSunday);


            //将本月第一天和最后一天封装到map集合中
            Map<String,Object> paramMonth = new HashMap<String,Object>();
            paramMonth.put("monthFirst",monthFirst);
            paramMonth.put("monthLast",monthLast);

            // （1）今日新增会员数
            int todayNewMember = memberMapper.getTodayNewMember(today);
            // （2）总会员数
            int totalMember = memberMapper.getTotalMember();
            // （3）本周新增会员数
            int thisWeekNewMember = memberMapper.getThisWeekNewMember(paramWeek);
            // （4）本月新增会员数
            int thisMonthNewMember = memberMapper.getThisMonthNewMember(paramMonth);
            // （5）今日预约数
            int todayOrderNumber = orderMapper.getTodayOrderNumber(today);
            // （6）今日出游数
            int todayVisitsNumber = orderMapper.getTodayVisitsNumber(today);
            // （7）本周预约数
            int thisWeekOrderNumber = orderMapper.getThisWeekOrderNumber(paramWeek);
            // （9）本月预约数
            int thisMonthOrderNumber = orderMapper.getThisMonthOrderNumber(paramMonth);
            // （8）本周出游数
            int thisWeekVisitsNumber = orderMapper.getThisWeekVisitsNumber(paramWeek);
            // （10）本月出游数
            int thisMonthVisitsNumber = orderMapper.getThisMonthVisitsNumber(paramMonth);
            // （11）热门套餐
            List<Map<String,Object>> hotSetmeal = orderMapper.findHotSetmeal();


            map = new HashMap<String,Object>();
            map.put("reportDate",today);
            map.put("todayNewMember",todayNewMember);
            map.put("totalMember",totalMember);
            map.put("thisWeekNewMember",thisWeekNewMember);
            map.put("thisMonthNewMember",thisMonthNewMember);
            map.put("todayOrderNumber",todayOrderNumber);
            map.put("todayVisitsNumber",todayVisitsNumber);
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
            map.put("hotSetmeal",hotSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
